package com.czy.workflow.core.context;

import com.czy.workflow.core.executor.TaskExecutor;
import com.czy.workflow.core.listener.FlowListener;
import com.czy.workflow.core.listener.FlowListenerNotify;
import com.czy.workflow.core.service.FlowService;
import com.czy.workflow.core.validator.FlowValidator;

import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ExecutorService;

/**
 * Created by admin on 2019/8/31.
 */
public class FlowContext {

    private Map<String, Object> executorList = new HashMap<>();
    private Map<String, Method> executorMethodList = new HashMap<>();
    private Map<String, FlowService> flowServiceImplMap = new HashMap<>();

    private List<FlowValidator> validatorList = new ArrayList<>();
    private List<FlowListener> listenerList = new ArrayList<>();

    private FlowConfig flowConfig;

    private ExecutorService threadPoolService;

    private FlowListenerNotify taskListenerNotify;

    private TaskExecutor taskExecutor;

    private FlowService flowService;

    public FlowContext(FlowConfig flowConfig, TaskExecutor taskExecutor,  ExecutorService threadPoolService, FlowListenerNotify taskListenerNotify, FlowService flowService) {
        this.flowConfig = flowConfig;
        this.taskExecutor = taskExecutor;
        this.threadPoolService = threadPoolService;
        this.taskListenerNotify = taskListenerNotify;
        this.flowService = flowService;
    }

    public boolean addExecutor(String flowCode, Object bean){
        executorList.put(flowCode, bean);
        return true;
    }

    public boolean addMethod(String flowCode, String nodeCode, Method method){
        String key = key(flowCode, nodeCode);
        executorMethodList.put(key, method);
        return true;
    }

    public boolean addFlowService(String name, FlowService bean){
        flowServiceImplMap.put(name, bean);
        return true;
    }

    public boolean addFlowValidator(FlowValidator validator){
        validatorList.add(validator);
        return true;
    }

    public boolean addFlowListener(FlowListener listener){
        listenerList.add(listener);
        return true;
    }

    public Object getExecutor(String flowCode){
        return executorList.get(flowCode);
    }

    public Method getMethod(String flowCode, String nodeCode){
        String key = key(flowCode, nodeCode);
        return executorMethodList.get(key);
    }

    public TaskExecutor getTaskExecutor(){
        return this.taskExecutor;
    }

    public FlowListenerNotify getTaskListenerNotify(){
        return this.taskListenerNotify;
    }

    public ExecutorService getThreadPoolService(){
        return this.threadPoolService;
    }

    public FlowService getFlowService() {
        return this.flowService;
    }

    public FlowConfig getFlowConfig(){
        return this.flowConfig;
    }

    public List<FlowValidator> getValidatorList() {
        return validatorList;
    }

    public List<FlowListener> getListenerList() {
        return listenerList;
    }

    public Collection<Method> getExecutorMethodList(){
        return executorMethodList.values();
    }

    private String key(String code1, String code2){
        return code1 + "." + code2;
    }
}
